package cn.com.connected.service.impl;

import cn.com.connected.common.emnus.StatusType;
import org.springframework.stereotype.Service;
import cn.com.connected.service.DynamicCommentService;
import cn.com.connected.base.params.page.PageParam;
import cn.com.connected.base.params.page.BaseSearchDto;
import cn.com.connected.base.params.page.PageResult;
import cn.com.connected.base.utils.BeanCopyUtils;
import cn.com.connected.dto.DynamicCommentDto;
import cn.com.connected.dto.search.DynamicCommentSearchDto;
import cn.com.connected.entity.DynamicCommentEntity;
import cn.com.connected.mapper.DynamicCommentMapper;
import cn.com.connected.vo.DynamicCommentVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

/**
 * 动态评论
 *
 * @author sms
 * @email sms@live.com
 * @date 2025-04-10 16:15:14
 */
@Slf4j
@Service("dynamicCommentService")
public class DynamicCommentServiceImpl extends ServiceImpl<DynamicCommentMapper, DynamicCommentEntity> implements DynamicCommentService {
    @Resource
    private DynamicCommentMapper dynamicCommentMapper;

    @Override
    public DynamicCommentVo dynamicCommentAdd(DynamicCommentDto dynamicCommentDto) {
        DynamicCommentEntity entity = new DynamicCommentEntity();
        BeanCopyUtils.copy(dynamicCommentDto,entity);
        dynamicCommentMapper.insert(entity);

        return dynamicCommentInfo(entity.getId());
    }

    @Override
    public void dynamicCommentEdit(String id, DynamicCommentDto dynamicCommentDto) {
        DynamicCommentEntity entity = dynamicCommentMapper.selectById(id);
        BeanCopyUtils.copy(dynamicCommentDto,entity);
        entity.setId(id);
        dynamicCommentMapper.updateById(entity);
    }

    @Override
    public DynamicCommentVo dynamicCommentInfo(String id) {
        DynamicCommentEntity entity = dynamicCommentMapper.selectById(id);
        return BeanCopyUtils.copy(entity,new DynamicCommentVo());
    }

    @Override
    public void dynamicCommentDel(String id) {
        DynamicCommentEntity entity = dynamicCommentMapper.selectById(id);
        dynamicCommentMapper.updateById(entity);
    }

    @Override
    public PageResult<DynamicCommentVo> dynamicCommentPage(BaseSearchDto<DynamicCommentSearchDto> searchParams) {
        LambdaQueryWrapper<DynamicCommentEntity> wrapper = Wrappers.lambdaQuery();

//        wrapper.eq(DynamicCommentEntity::getStatus),StatusType.normal.getValue());

        PageParam pageParam = searchParams.getPageParam();
        PageHelper.startPage(pageParam.getPageIndex(), pageParam.getPageSize());
        DynamicCommentSearchDto searchDto = searchParams.getSearchOption();

//        wrapper.orderByDesc(Arrays.asList(DynamicCommentEntity::getCreateTime));

        Page<DynamicCommentEntity> page = (Page)dynamicCommentMapper.selectList(wrapper);

        PageResult<DynamicCommentVo> pageResult = BeanCopyUtils.copyPageResult(page, DynamicCommentVo.class);

        return pageResult;
    }

}
